#include <fmt/core.h>
#include <unistd.h>
#include "RingQueue.hpp"

struct threaddata
{
    RingQueue<int> *_rq;
    std::string _name;
};

void *consumer(void *args)
{
    threaddata *td = static_cast<threaddata *>(args);
    while (true)
    {
        sleep(3);
        int t = 0;
        td->_rq->Pop(&t);
        fmt::println("{} 消费者拿到一个数据: {}", td->_name, t);
    }
    return nullptr;
}

int data = 1;

void *productor(void *args)
{
    threaddata *td = static_cast<threaddata *>(args);
    while (true)
    {
        sleep(1);
        fmt::println("{} 生产了一个任务: {}", td->_name, data);
        td->_rq->Equeue(data);
        data++;
    }
    return nullptr;
}

int main()
{
    RingQueue<int> *rq = new RingQueue<int>();

    pthread_t c[2], p[3];

    threaddata *td = new threaddata();
    td->_name = "cthread-1";
    td->_rq = rq;
    pthread_create(c, nullptr, consumer, td);

    threaddata *td2 = new threaddata();
    td2->_name = "cthread-2";
    td2->_rq = rq;
    pthread_create(c + 1, nullptr, consumer, td2);

    threaddata *td3 = new threaddata();
    td3->_name = "pthread-3";
    td3->_rq = rq;
    pthread_create(p, nullptr, productor, td3);

    threaddata *td4 = new threaddata();
    td4->_name = "pthread-4";
    td4->_rq = rq;
    pthread_create(p + 1, nullptr, productor, td4);

    threaddata *td5 = new threaddata();
    td5->_name = "pthread-5";
    td5->_rq = rq;
    pthread_create(p + 2, nullptr, productor, td5);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);

    return 0;
}